package org.seven.jrdp.web.mgt.sys;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.seven.jrdp.biz.sys.model.SysOrgan;
import org.seven.jrdp.biz.sys.service.SysOrganService;
import org.seven.jrdp.commons.model.Result;
import org.seven.jrdp.commons.model.TreeNode;
import org.seven.jrdp.commons.shiro.model.User;
import org.seven.jrdp.commons.util.ShiroUtils;

@RestController
@RequestMapping("/mgt/sys/organ")
public class SysOrganController {
	@Autowired
	private SysOrganService sysOrganService;

	@GetMapping("/{view}")
	public ModelAndView view(@PathVariable String view) {
		return new ModelAndView("mgt/sys/organ/organ_" + view);
	}

	@PostMapping("/find")
	public List<SysOrgan> find() {
		List<SysOrgan> list = sysOrganService.find();
		return list;
	}

	@RequestMapping("/get")
	public SysOrgan get(String id) {
		SysOrgan sysOrgan = sysOrganService.get(id);
		return sysOrgan;
	}

	@PostMapping("/insert")
	public Result insert(SysOrgan sysOrgan) {
		sysOrganService.insert(sysOrgan);
		return Result.SUCCESS;
	}

	@PostMapping("/update")
	public Result update(SysOrgan sysOrgan) {
		sysOrganService.update(sysOrgan);
		return Result.SUCCESS;
	}

	@PostMapping("/delete")
	public Result delete(String id) {
		sysOrganService.delete(id);
		return Result.SUCCESS;
	}

	@RequestMapping("/tree")
	public List<TreeNode> tree() {
		List<TreeNode> list = sysOrganService.findTree();
		return list;
	}

	@RequestMapping("/tree/user")
	public List<TreeNode> user() {
		List<TreeNode> list = null;
		User user = ShiroUtils.getUser();
		if (user.isAdmin()) {
			list = sysOrganService.findTree();
		} else {
			list = sysOrganService.findTreeByCode(user.getOrgan().getCode() + "%");
		}
		return list;
	}
}